package com.lelefly.base.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.lelefly.base.model.Result;
import com.lelefly.base.po.VersionUpdate;
import com.lelefly.base.serviceImp.VersionService;
import com.lelefly.constant.Constant;
import com.lelefly.handleexecption.ServiceExecption;


@Controller
@RequestMapping("/update")
public class DownLoadContraller extends BaseController {
	
	@Resource
	private VersionService vsesionService;
	
	@RequestMapping("/download")
	@ResponseBody
	public String download(HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = "Lelefly.apk";
		response.reset();
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		try {
			String path = request.getServletContext().getRealPath("/WEB-INF/")
					+ "/Assest";//这个download目录为啥建立在classes下的
			InputStream inputStream = new FileInputStream(new File(path
					+ File.separator + fileName));
			response.setContentLength(inputStream.available());
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			 // 这里主要关闭。
			os.close();
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "ok";
	}
	
	
	
	@RequestMapping("/upload")
	@ResponseBody
	public Result upload(@RequestParam(value = "file",required = false) MultipartFile file ,HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = "Lelefly.apk";
		String versoncode = request.getParameter("versioncode");
		Result result = null;
		try {
			 result = vsesionService.saveVsesionService(versoncode);
			 DefaultMultipartHttpServletRequest  multipartRequest =(DefaultMultipartHttpServletRequest ) request;
			 InputStream is = file.getInputStream();
			 if (is ==null) {
				throw new ServiceExecption("上传文件不能为空");
			  }
			String path = request.getServletContext().getRealPath("/WEB-INF/")
					+ "/Assest";//这个download目录为啥建立在classes下的
			File file2 = new File(path+ File.separator + fileName);
			if (file2.exists()) {
				file2.delete();
			}
			OutputStream os = new FileOutputStream(new File(path+ File.separator + fileName));
			byte[] b = new byte[2048];
			int length;
			while ((length = is.read(b)) > 0) {
				os.write(b, 0, length);
			}
			 // 这里主要关闭。
			os.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result<>();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}catch (ServiceExecption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = new Result<>();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes(e.getMessage());
		}
		return result;
	}
	
	
	@RequestMapping(value="/getLastVersion",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result getLastVersion(){
		Result<VersionUpdate> result=null;
		try {
			result = vsesionService.getVsesionService();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			getLogger().info("----->>"+e.getLocalizedMessage());
		}
		return result;
	}

	
	
}
